/**
* JQuery Twitter Feed. 
*
* @author Tom Elliott @ www.webdevdoor.com (2013) based on https://twitter.com/javascripts/blogger.js
* @author-edited eThemes.com
* Requires JSON output from authenticating script: http://www.webdevdoor.com/php/authenticating-twitter-feed-timeline-oauth
*/

// ------------ Twitter Feed Variables	------------	
var totaltweets 		= 12; //Must be a multiple of tweetshift	
var tweetshift 			= 3;
var twitterprofile 		= "e_themes";
var screenname 			= "Abdullah Bakhsh";
var showdirecttweets	= false;
var showretweets 		= false;
var showtweetlinks 		= true;
var showprofilepic 		= false;
var showtweetactions	= false;
var showretweetindicator= false;
var feedheight 			= 150; 
var pausetime 			= 5000;
var slidetime 			= 2000;
var slideinitial 		= true;
	
$(document).ready(function () {
	
	
	
	var headerHTML 	= '';
	var loadingHTML = '';
	headerHTML 		+= '<div id="twitter-header"></a>';
	loadingHTML 	+= '<div id="loading-container" style="color:#fff;height:'+feedheight+'px;">Loading...</div>';
	
	$('#twitter-feed').html(headerHTML + loadingHTML);
	 
    $.getJSON('http://localhost/Dale/twitterAPI/get-tweets.php', 
    	function(feeds) {  
          	var feedHTML 		= '';
          	var displayCounter	= 1;
			         
        	for (var i=0; i<feeds.length; i++) {
				var tweetscreenname	= feeds[i].user.name;
                var tweetusername	= feeds[i].user.screen_name;
                var profileimage 	= feeds[i].user.profile_image_url_https;
                var status 			= feeds[i].text; 
				var isaretweet 		= false;
				var isdirect 		= false;
				var tweetid 		= feeds[i].id_str;
				
				//If the tweet has been retweeted, get the profile pic of the tweeter
				if(typeof feeds[i].retweeted_status != 'undefined') {
					profileimage	= feeds[i].retweeted_status.user.profile_image_url_https;
					tweetscreenname	= feeds[i].retweeted_status.user.name;
					tweetusername	= feeds[i].retweeted_status.user.screen_name;
					tweetid			= feeds[i].retweeted_status.id_str;
					status			= feeds[i].retweeted_status.text; 
					isaretweet		= true;
				}
				
				 //Check to see if the tweet is a direct message
				if (feeds[i].text.substr(0,1) == "@")
					isdirect = true;
				 
				 var pb = "";
				 if (showretweetindicator == true) {
					pb = 'style="padding-bottom:20px;"';
				 }

				 //Generate twitter feed HTML based on selected options
				 if (((showretweets == true) || ((isaretweet == false) && (showretweets == false))) && ((showdirecttweets == true) || ((showdirecttweets == false) && (isdirect == false)))) { 
					if ((feeds[i].text.length > 1) && (displayCounter <= totaltweets)) {             
						if (showtweetlinks == true) {
							status = addlinks(status);
						}
						 
						if (displayCounter == 1) {
							feedHTML += headerHTML;
							feedHTML += '<div class="tweets-container" style="height:'+feedheight+'px;">';
						}
						
						var active = i == 0 ? 'active' : '';
						
						feedHTML += '<div class="twitter-article '+active+'" id="t'+displayCounter+'" '+pb+'>'; 										                 
						feedHTML += '<i class="fa fa-twitter"></i><div class="twitter-text"><p>'+status+'<span class="tweet-time"><a href="https://twitter.com/'+tweetusername+'/status/'+tweetid+'" target="_blank">- '+relative_time(feeds[i].created_at)+'</a></span></p>';
						
						if ((isaretweet == true) && (showretweetindicator == true)) {
							feedHTML += '<div id="retweet-indicator"></div>';
						}						
						if (showtweetactions == true) {
							feedHTML += '<div id="twitter-actions"><div class="intent" id="intent-reply"><a href="https://twitter.com/intent/tweet?in_reply_to='+tweetid+'" title="Reply"></a></div><div class="intent" id="intent-retweet"><a href="https://twitter.com/intent/retweet?tweet_id='+tweetid+'" title="Retweet"></a></div><div class="intent" id="intent-fave"><a href="https://twitter.com/intent/favorite?tweet_id='+tweetid+'" title="Favourite"></a></div></div>';
						}
						
						feedHTML += '</div>';
						feedHTML += '</div>';
						
						displayCounter++;
					}   
				 }
            }
			feedHTML += '</div>';
             
            $('#twitter-feed').html(feedHTML);
			
			// Hook up the carousel, if its included
			 if ( $.isFunction($.fn.owlCarousel) ) {
				$(".tweets-container").owlCarousel({
					singleItem:true,
					autoPlay:true,
					slideSpeed : 400
				});
			} else {
				console.log("Link owlCarousel to allow sliding in between Tweets!");	
			}
			
		
			$('#twitter-actions a').click(function(e){
				var url = $(this).attr('href');
				window.open(url, 'tweet action window', 'width=580,height=500');
				e.preventDefault();
			}); 
			
    }).error(function(jqXHR, textStatus, errorThrown) {
		var error = "";
			 if (jqXHR.status === 0) {
               error = 'Connection problem. Check file path and www vs non-www in getJSON request';
            } else if (jqXHR.status == 404) {
                error = 'Requested page not found. [404]';
            } else if (jqXHR.status == 500) {
                error = 'Internal Server Error [500].';
            } else if (exception === 'parsererror') {
                error = 'Requested JSON parse failed.';
            } else if (exception === 'timeout') {
                error = 'Time out error.';
            } else if (exception === 'abort') {
                error = 'Ajax request aborted.';
            } else {
                error = 'Uncaught Error.\n' + jqXHR.responseText;
            }	
       		console.log("error: " + error);
    });
    

    //Function modified from Stack Overflow
    function addlinks(data) {
        //Add link to all http:// links within tweets
         data = data.replace(/((https?|s?ftp|ssh)\:\/\/[^"\s\<\>]*[^.,;'">\:\s\<\>\)\]\!])/g, function(url) {
            return '<a href="'+url+'" >'+url+'</a>';
        });
             
        //Add link to @usernames used within tweets
        data = data.replace(/\B@([_a-z0-9]+)/ig, function(reply) {
            return '<a href="http://twitter.com/'+reply.substring(1)+'" style="font-weight:lighter;" target="_blank">'+reply.charAt(0)+reply.substring(1)+'</a>';
        });
		//Add link to #hastags used within tweets
        data = data.replace(/\B#([_a-z0-9]+)/ig, function(reply) {
            return '<a href="https://twitter.com/search?q='+reply.substring(1)+'" style="font-weight:lighter;" target="_blank">'+reply.charAt(0)+reply.substring(1)+'</a>';
        });
        return data;
    }
     
    function relative_time(time_value) {
      var values = time_value.split(" ");
      time_value = values[1] + " " + values[2] + ", " + values[5] + " " + values[3];
      var parsed_date = Date.parse(time_value);
      var relative_to = (arguments.length > 1) ? arguments[1] : new Date();
      var delta = parseInt((relative_to.getTime() - parsed_date) / 1000);
	  var shortdate = time_value.substr(4,2) + " " + time_value.substr(0,3);
      delta = delta + (relative_to.getTimezoneOffset() * 60);
     
      if (delta < 60) {
        return '1m ago';
      } else if(delta < 120) {
        return '1m ago';
      } else if(delta < (60*60)) {
        return (parseInt(delta / 60)).toString() + 'm ago';
      } else if(delta < (120*60)) {
        return '1h ago';
      } else if(delta < (24*60*60)) {
        return (parseInt(delta / 3600)).toString() + 'h ago';
      } else if(delta < (48*60*60)) {
        //return '1 day';
		return shortdate;
      } else {
        return shortdate;
      }
    }
     
});